<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend> 查 询 信 息 </legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="accountid" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 查 询 </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>-->
<!--                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删 除 </button>-->
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
<!--            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>-->
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删 除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8080/admin/diaries/allDiary',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                // {type: "checkbox", width: 70},
                {field: 'contentid', width: 120, title: '日记ID', sort: true},
                {field: 'content', width: 150, title: '日记内容'},
                {field: 'accountid', width: 110, title: '用户ID'},
                {field: 'name', width: 110, title: '用户名'},
                {field: 'tagid', width: 110, title: '标签ID'},
                {field: 'province', title: '省份', width: 110},
                {field: 'city', width: 110, title: '城市'},
                {field: 'distinct', width: 120, title: '区（县）'},
                {field: 'goodnum', width: 120, title: '点赞数', sort: true},
                {field: 'title', width: 110, title: '标题'},
                {field: 'diarydate', width: 110, title: "发布日期"},

                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            // var id =  $('#accountid').val();
            // $.get("selectuser",id,function (data) {
            //     console.log(data);
            // })

            var id =  $('#accountid').val();

            if (id===''){
                table.render({
                    elem: '#currentTableId',
                    url: 'http://localhost:8080/admin/diaries/allDiary',
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        // {type: "checkbox", width: 70},
                        {field: 'contentid', width: 120, title: '日记ID', sort: true},
                        {field: 'content', width: 150, title: '日记内容'},
                        {field: 'accountid', width: 110, title: '用户ID'},
                        {field: 'name', width: 110, title: '用户名'},
                        {field: 'tagid', width: 110, title: '标签ID'},
                        {field: 'province', title: '省份', width: 110},
                        {field: 'city', width: 110, title: '城市'},
                        {field: 'distinct', width: 120, title: '区（县）'},
                        {field: 'goodnum', width: 120, title: '点赞数', sort: true},
                        {field: 'title', width: 110, title: '标题'},
                        {field: 'diarydate', width: 110, title: "发布日期"},

                        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]],
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line'
                });
            }

            if (id!==''){
                table.render({
                    elem: '#currentTableId',
                    url: 'http://localhost:8080/admin/diaries/diaryList/'+id,
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        // {type: "checkbox", width: 70},
                        {field: 'contentid', width: 120, title: '日记ID', sort: true},
                        {field: 'content', width: 150, title: '日记内容'},
                        {field: 'accountid', width: 110, title: '用户ID'},
                        {field: 'name', width: 110, title: '用户名'},
                        {field: 'tagid', width: 110, title: '标签ID'},
                        {field: 'province', title: '省份', width: 110},
                        {field: 'city', width: 110, title: '城市'},
                        {field: 'distinct', width: 120, title: '区（县）'},
                        {field: 'goodnum', width: 120, title: '点赞数', sort: true},
                        {field: 'title', width: 110, title: '标题'},
                        {field: 'diarydate', width: 110, title: "发布日期"},

                        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]],
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line'
                });
            }
            // $.get("http://localhost:8080/admin/diaries/diaryList/"+id,function (data) {
            //    // console.log(data);
            //    //  layer.alert(JSON.stringify(data.data,null,'\n'), {
            //    //      title: '日记信息'
            //    //  });
            //
            //
            // })
            // layer.open({
            //     type: 2,
            //     title: "搜索信息",
            //     btn: [ '确认'],
            //     area:["40%","35%"],
            //     content: ["http://localhost:8080/admin/selectuser/"+id],
            //         });
            // });
//执行搜索重载

            // table.reload('currentTableId', {
            //     url: 'http://localhost:8080/admin/diaries/allDiary'
            //     ,page: {
            //         curr: 1
            //     }
            //     ,where:{
            //         id:$('#accountid').val()
            //
            //     }
            //     ,done: function () {
            //         this.where={};
            //     }
            // }, 'data');
            // 搜索
            // console.log($('#accountid').val());
            // table.render();
            return false;

        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
         if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
            layer.confirm('真的要删除吗？', function (index) {
                $.post('/admin/diaries/deleteDiary',{contentid: obj.data.contentid},function (data){
                    obj.del();
                    layer.msg(data.msg);
                    layer.close(index);
                });
            });
            }
        });

    });
</script>
